package com.invite.me.beans.presenter;

import android.util.Log;


import com.google.gson.Gson;
import com.invite.me.api.UrlConstants;
import com.invite.me.beans.fabean.NameattestationfaBean;
import com.invite.me.beans.fabean.ShimingRzCBean;
import com.invite.me.beans.res.NameAtteastationtpBean;
import com.invite.me.beans.res.NameattestationBean;
import com.invite.me.ui.cehualan.NameAttestationActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.invite.me.wlqq.RxJavaDataImpl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class NameAttestationPresenter implements Contract.BasePresenter {
    NameAttestationActivity splashActivity;

    public NameAttestationPresenter(NameAttestationActivity signInActivity) {
        this.splashActivity = signInActivity;
        splashActivity.setPresenter(this);
    }


    @Override
    public void start() {

    }

    //查询实名认证
    public void shilie(){
        HashMap<String,Object> headrMap = new HashMap<>();
      //  headrMap.put("Content-Type","multipart/form-data");
        headrMap.put("userId",SspPrefUtils.getString(Constants.SSPTOKEN, ""));


        //   headrMap.put("Content-Type","application/json");
        //传参解析Bean
//        ShiLieChuanZhiBean presenterCanShuGongBean= new ShiLieChuanZhiBean();
//        presenterCanShuGongBean.setPager(pager);
//        presenterCanShuGongBean.setShuliang(shuliang);
//
//        String s = new Gson().toJson(presenterCanShuGongBean);
        //    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json,charset-UTF-8"),s);
        RxJavaDataImpl.getData(UrlConstants.API_SMRZ, headrMap, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        ShimingRzCBean gankInfo = gson.fromJson(str, ShimingRzCBean.class);
                        splashActivity.onDataSucess(gankInfo);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    //图片转换
    public void shangchuanweijian(File file, String type){
        Map<String,Object> headrMap = new HashMap<>();
      //     headrMap.put("Content-Type","multipart/form-data");
           headrMap.put("userId",SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        //   headrMap.put("userId","78ef0eeb3816f5e9abb77c9fc68e4c1b");
//        CeBeann logeInDataBean = new CeBeann();
//        logeInDataBean.setType(2);
//
//        Gson gson = new Gson();
//        String phoneNumber = gson.toJson(logeInDataBean);
//        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),requestFile);
        MultipartBody.Part namebody = MultipartBody.Part.createFormData("type","1");
//        MultipartBody.Part part = MultipartBody.Part.create(requestBody);
//        MultipartBody multipartBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)
//                .addFormDataPart("type", 1)//添加表单数据
//                .build();


        RxJavaDataImpl.uploadFiles(UrlConstants.API_SCTP,headrMap,body,namebody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {

                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameAtteastationtpBean gankInfo = gson.fromJson(str, NameAtteastationtpBean.class);
                        splashActivity.onDataSucess(gankInfo);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onError(Throwable e) {

                splashActivity.onDataFaile(e.getMessage());
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });


    }



    //提交实名认证信息
    public void tijiaoshimingrenzheng(String name,String shenfen,String zhengmian,String fanmian){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        NameattestationfaBean logeInDataBean = new NameattestationfaBean();
        logeInDataBean.setName(name);
        logeInDataBean.setIdCard(shenfen);
        logeInDataBean.setIdCardFrontImg(zhengmian);
        logeInDataBean.setIdCardBackImg(fanmian);
        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.postData(UrlConstants.API_TJSMRZ, hashMap, requestBody,new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameattestationBean gankInfo = gson.fromJson(str, NameattestationBean.class);
                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }

}
